package my.assistant.homecashmanager.account.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import my.assistant.homecashmanager.account.data.AccountBank;
import my.assistant.homecashmanager.account.data.AccountBase;
import my.assistant.homecashmanager.account.data.AccountCash;
import my.assistant.homecashmanager.account.data.AccountDebt;

import org.assistant.common.jdbc.core.RowMapper;
import org.assistant.common.jdbc.dao.BasicDao;

public class AccountDaoImpl extends BasicDao implements AccountDao {
	private class AccountBaseRowMapper implements RowMapper<AccountBase> {

		public AccountBase mapRow(ResultSet rs, int index) throws SQLException {
			AccountBase accountBase = new AccountBase();
			accountBase.setId(rs.getString("account_id_"));
			accountBase.setName(rs.getString("account_name_"));
			accountBase.setExplain(rs.getString("explain_"));
			accountBase.setType(rs.getString("account_type_"));
			accountBase.setMoneyType(rs.getString("money_type_"));
			accountBase.setBalance(rs.getBigDecimal("balance_"));
			accountBase.setUserId(rs.getString("user_id_"));
			accountBase.setHomeId(rs.getString("home_id_"));
			accountBase.setLatestDealDate(rs.getDate("latest_deal_date_"));
			accountBase.setStatus(rs.getBoolean("status_"));

			return accountBase;
		}
	}

	public List<AccountBase> getAccountBaseListByUser(String userId)
			throws SQLException {
		String sql = "select account_id_,account_name_,explain_,account_type_,money_type_,balance_,user_id_,home_id_,latest_deal_date_,status_ from account_base a where user_id_=? order by order_";
		List<AccountBase> list = getJdbcTemplate().queryForList(
			sql, new AccountBaseRowMapper(), userId);
		return list;
	}

	public List<AccountBase> getAccountBaseListByHome(String homeId)
			throws SQLException {
		String sql = "select account_id_,account_name_,explain_,account_type_,money_type_,balance_,user_id_,home_id_,latest_deal_date_,status_ from account_base a where home_id_=? order by order_";
		List<AccountBase> list = getJdbcTemplate().queryForList(
			sql, new AccountBaseRowMapper(), homeId);
		return list;
	}

	private void insertAccountBase(AccountBase accountBase) throws SQLException {
		StringBuffer sqlBuff = new StringBuffer();
		sqlBuff
			.append("insert into account_base(account_id_,account_name_,explain_,account_type_,money_type_,balance_,user_id_,home_id_,latest_deal_date_,status_)");
		sqlBuff.append(" values(?,?,?,?,?,?,?,?,?,?,now())");

		getJdbcTemplate().update(
			sqlBuff.toString(), accountBase.getId(), accountBase.getName(),
			accountBase.getExplain(), accountBase.getType(),
			accountBase.getMoneyType(), accountBase.getBalance(),
			accountBase.getUserId(), accountBase.getHomeId(),
			accountBase.getLatestDealDate(), accountBase.isStatus());
	}

	public void insertAccount(AccountCash accountCash) throws SQLException {
		StringBuffer sqlBuff = new StringBuffer();
		sqlBuff
			.append("insert into account_ext(account_id_,account_type_,creater_,create_date_)");
		sqlBuff.append(" values(?,?,?,now())");

		// TODO 下面两句应该包含在一个事务中
		insertAccountBase(accountCash);
		getJdbcTemplate().update(
			sqlBuff.toString(), accountCash.getId(), accountCash.getType(),
			accountCash.getCreater());
	}

	public void insertAccount(AccountBank accountBank) throws SQLException {
		StringBuffer sqlBuff = new StringBuffer();
		sqlBuff
			.append("insert into account_ext(account_id_,account_type_,bank_code_,client_no_,account_no_,card_no_,creater_,create_date_)");
		sqlBuff.append(" values(?,?,?,?,?,?,?,now())");

		// TODO 下面两句应该包含在一个事务中
		insertAccountBase(accountBank);
		getJdbcTemplate().update(
			sqlBuff.toString(), accountBank.getId(), accountBank.getType(),
			accountBank.getBankCode(), accountBank.getClientNo(),
			accountBank.getAccountNo(), accountBank.getCardNo(),
			accountBank.getCreater());
	}

	public void insertAccount(AccountDebt accountDebt) throws SQLException {
		StringBuffer sqlBuff = new StringBuffer();
		sqlBuff
			.append("insert into account_ext(account_id_,account_type_,creater_,create_date_)");
		sqlBuff.append(" values(?,?,?,now())");

		// TODO 下面两句应该包含在一个事务中
		insertAccountBase(accountDebt);
		getJdbcTemplate().update(
			sqlBuff.toString(), accountDebt.getId(), accountDebt.getType(),
			accountDebt.getCreater());
	}
}
